package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* AlarmClockView4
* */
class AlarmClockView4 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_13D30C)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            save()
            mPaint0.strokeWidth = (cx / 10).coerceAtLeast(context.resources.getDimension(R.dimen.dp_1))
            mPath0.apply {
                val sin45 = sinValue(45.0)
                val con45 = conValue(45.0)
                moveTo(0F, -cy * 7 / 20)
                lineTo(0F, 0F)
                lineTo((cx * 7 / 10) * con45 - cx * 5 / 20, (cx * 7 / 10) * sin45 - cy * 5 / 20)
            }

            mPath1.apply {
                addCircle(
                    0F,
                    0F,
                    cx * 7 / 10,
                    Path.Direction.CW
                )
            }

            mPath2.apply {
                fillType = Path.FillType.INVERSE_WINDING
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )
                addPath(mPath0)
                clipPath(mPath1)
            }
            drawPath(mPath2, mPaint0)
            restore()

            val sin135 = sinValue(135.0)
            val con135 = conValue(135.0)
            drawLine(
                (cx * 7 / 10) * con135,
                (cx * 7 / 10) * sin135,
                cx * 9 / 10 * con135,
                cx * 9 / 10 * sin135,
                mPaint0
            )

            val sin45 = sinValue(45.0)
            val con45 = conValue(45.0)

            drawLine(
                (cx * 7 / 10) * con45,
                (cx * 7 / 10) * sin45,
                cx * 9 / 10 * con45,
                cx * 9 / 10 * sin45,
                mPaint0
            )

            val sina0 = sinValue(215.0)
            val cosa0 = conValue(215.0)

            val sina1 = sinValue(235.0)
            val cosa1 = conValue(235.0)

            drawLine(
                cx * 9 / 10 * cosa0,
                cy * 9 / 10 * sina0,
                cx * 9 / 10 * cosa1,
                cy * 9 / 10 * sina1,
                mPaint0
            )

            val sinb0 = sinValue(305.0)
            val cosb0 = conValue(305.0)

            val sinb1 = sinValue(325.0)
            val cosb1 = conValue(325.0)

            drawLine(
                cx * 9 / 10 * cosb0,
                cy * 9 / 10 * sinb0,
                cx * 9 / 10 * cosb1,
                cy * 9 / 10 * sinb1,
                mPaint0
            )

            restore()
        }
    }

    private fun conValue(angle: Double) : Float {
        return Math.cos(Math.toRadians(angle)).toFloat()
    }

    private fun sinValue(angle: Double) : Float {
        return Math.sin(Math.toRadians(angle)).toFloat()
    }
}